local focus = 0
local talk_start = 0
local target = 0
local following = false
local attacking = false

function onThingMove(creature, thing, oldpos, oldstackpos)

end

local welcome = {"Nice haircut", "Enjoy playing", "You may be lucky to see the God today", "The depot is to the East", "If you are here because you are dead, do not die any more"}
function onCreatureAppear(creature)
		selfSay (' Welcome to MagichcigaM. ' .. welcome[math.random(1, 5)] .. '.')
end

function onCreatureDisappear(cid, pos)
	if focus == cid then
		selfSay('Good bye then.')
		focus = 0
		talk_start = 0
	end
end


function onCreatureTurn(creature)
	
end


function msgcontains(txt, str)
return (string.find(txt, str) and not string.find(txt, '(%w+)' .. str) and not string.find(txt, str .. '(%w+)'))
end


function onCreatureSay(cid, type, msg)
msg = string.lower(msg)

if (msgcontains(msg, 'hi') and (focus == 0)) and getDistanceTo(cid) < 4 then
selfSay('Hello ' .. getCreatureName(cid) .. '. Do you want me to answer you a yes/no question?')
focus = cid
talk_start = os.clock()

elseif msgcontains(msg, 'hi') and (focus ~= cid) and getDistanceTo(cid) < 4 then
selfSay('Sorry, ' .. getCreatureName(cid) .. '! I talk to you in a minute.')

elseif focus == cid then
talk_start = os.clock()

if msgcontains(msg, 'yes') then
selfSay('Ask your question!')
talk_state = 1 
end

if talk_state == 1 and msgcontains(msg, '?') then
if math.random(1,2) == 1 then
selfSay('Yes.')
elseif math.random(1,2) == 2 then
selfSay('No.')
talk_state = 1 
end

elseif msgcontains(msg, 'bye') and getDistanceTo(cid) < 4 then
selfSay('Good bye, ' .. getCreatureName(cid) .. '!')
focus = 0
talk_start = 0
end
end
end

function onThink()

if (os.clock() - talk_start) > 15 then
if focus > 0 then
selfSay('Next Please...')
end
focus = 0
end
if focus ~= 0 then
if getDistanceTo(focus) > 2 then
selfSay('Good bye then.')
focus = 0
end
end
end